package amst;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChart extends ApplicationFrame {

	private static final long serialVersionUID = -5234769463107478835L;
	
	private DefaultCategoryDataset dataset = null;

	private String title;
	
    public BarChart(final String title) {
        super(title);
        this.title = title;
        dataset = new DefaultCategoryDataset();
    }

    public void draw() {
      final JFreeChart chart = createChart(dataset);
      final ChartPanel chartPanel = new ChartPanel(chart);
      chartPanel.setPreferredSize(new Dimension(400, 500));
      setContentPane(chartPanel);
      
      pack();
      RefineryUtilities.centerFrameOnScreen(this);
      setVisible(true);
    }
    
    public void addPoint(final double value, final String serial, final String category) {
    	dataset.addValue(value, serial, category);
    }
    
    public void setValuePoint(final double value, final String serial, final String category) {
    	dataset.setValue(value, serial, category);
    }

    private JFreeChart createChart(final CategoryDataset dataset) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            title,         // chart title
            "Category",               // domain axis label
            "Units",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        chart.setBackgroundPaint(Color.white);
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
        
        return chart;
        
    }
   
    public static void main(final String[] args) {
    	BarChart chart = new BarChart("Hello");
    	chart.addPoint(1.34, "X1", "Y1");
    	chart.addPoint(2.18, "X2", "Y1");
    	chart.addPoint(0.97, "X3", "Y1");
    	
    	chart.draw();
    	for (int i = 0; i < 10; i++) {
    		chart.setValuePoint(1.34 + i*0.4, "X1", "Y1");
    		chart.setValuePoint(2.18 + i*0.3, "X2", "Y1");
    		chart.setValuePoint(0.97 + i*0.5, "X3", "Y1");
    		try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
    	
    	
    }

}
